// Vue
import Vue from 'vue'
import App from './App'
// 核心插件
import d2admin from '@/plugin/d2admin'
// store
import store from '@/store/index'
import axios from 'axios'
// 菜单和路由设置
import router from './router'
import menuHeader from '@/menu/header'
import menuAside from '@/menu/aside'
import { frameInRoutes } from '@/router/router'
// element-ui组件
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import animated from 'animate.css' // npm install animate.css --save安装，再引入
Vue.use(animated)
// Echarts
import VePie from 'v-charts/lib/pie.common'
Vue.component(VePie.name, VePie)
import { Message } from 'element-ui'
import Driective from './plugin/directive'
Vue.use(Driective)
Vue.prototype.$message = Message;
Vue.prototype.$axios = axios
import Viewer from 'v-viewer'
import 'viewerjs/dist/viewer.css'
Vue.use(Viewer);
Viewer.setDefaults({
  Options: { "inline": true, "button": true, "navbar": true, "title": true, "toolbar": true, "tooltip": true, "movable": true, "zoomable": true, "rotatable": true, "scalable": true, "transition": true, "fullscreen": true, "keyboard": true, "url": "data-source" }
});
Vue.use(ElementUI)
// 核心插件
Vue.use(d2admin)
import moment from 'moment'
import service from "@/plugin/axios";
import util from "@/libs/util";
Vue.filter('moment', function (value, formatString) {
  formatString = formatString || 'YYYY-MM-DD';
  return moment(value).format('YYYY-MM-DD HH:mm:ss');
});
// import Video from 'video.js'
// import 'video.js/dist/video-js.css'
// Vue.prototype.$video = Video
import VideoPlayer from 'vue-video-player'
require('video.js/dist/video-js.css')
require('vue-video-player/src/custom-theme.css')
Vue.use(VideoPlayer)
//axios实现所有请求自动携带token
axios.interceptors.request.use(
  config => {
    // 在发送请求之前做些什么
    let token = JSON.parse(localStorage.getItem('token'))
    if (token) {
      config.headers['Token'] = token
    }
    return config;
  },
  error => {
    Promise.reject(error);
  }
)
axios.interceptors.response.use(
  response => {
    const dataAxios = response.data.Status
    // 根据 code 进行判断
    if(dataAxios == 110){
      store.dispatch('d2admin/account/logout', {
        confirm: true
      })
    }
    return response
  },
)
var axios_instance = axios.create({
  transformRequest: [function (data) {
    data = Qs.stringify(data);
    return data;
  }],
});
Vue.filter('timeTo', function (value, s) {
  if (!value) return '-';
  let date = new Date();
  date.setTime(value * 1000);
  let y = date.getFullYear() - 2000;
  let m = date.getMonth() + 1;
  m = m < 10 ? ('0' + m) : m;
  let d = date.getDate();
  d = d < 10 ? ('0' + d) : d;
  let h = date.getHours();
  h = h < 10 ? ('0' + h) : h;
  let minute = date.getMinutes();
  let second = date.getSeconds();
  minute = minute < 10 ? ('0' + minute) : minute;
  second = second < 10 ? ('0' + second) : second;
  let time = '';
  s === 1 ? time = y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second : time = y + '-' + m + '-' + d;
  if (y < 0) {
    time = '无';
  }
  return time;
});
Vue.filter('base64ToStr', function (value) {
  return new Buffer(value, 'base64').toString();
});
Vue.prototype.sum = function (value) {
  let result = 0;
  for (let i = 0; i < value.length; i++) {
    if (!value[i]) value[i] = 0;
    result += value[i] * 1;
  }
  return result;
};
new Vue({
  router,
  store,
  render: h => h(App),
  created () {
    // 处理路由 得到每一级的路由设置
    this.$store.commit('d2admin/page/init', frameInRoutes)
    // 设置顶栏菜单
    this.$store.commit('d2admin/menu/headerSet', menuHeader)
    // 设置侧边栏菜单
    this.$store.commit('d2admin/menu/asideSet', menuAside)
    // 初始化菜单搜索功能
    this.$store.commit('d2admin/search/init', menuHeader)
  },
  mounted () {
    // 展示系统信息
    this.$store.commit('d2admin/releases/versionShow')
    // 用户登录后从数据库加载一系列的设置
    this.$store.dispatch('d2admin/account/load')
    // 获取并记录用户 UA
    this.$store.commit('d2admin/ua/get')
    // 初始化全屏监听
    this.$store.dispatch('d2admin/fullscreen/listen')
  }
}).$mount('#app')
